//
//
//

package qqopenapi_v3

import (
  "math/rand"
  "net/url"
  "sort"
  "time"
)

func pay_rep_value(src string) string {

  var t = make([]byte, 0)
  for i:=0; i<len(src); i++ {
    switch c := src[i]; {
    case src[i] >= 'a' && src[i] <= 'z':
      t = append(t, src[i])
    case src[i] >= 'A' && src[i] <= 'Z':
      t = append(t, src[i])
    case src[i] >= '0' && src[i] <= '9':
      t = append(t, src[i])
    case c == '!' || c == '*' || c == '(' || c == ')':
      t = append(t, src[i])
    default:
      t = append(t, '%')
      t = append(t, "0123456789ABCDEF"[c>>4])
      t = append(t, "0123456789ABCDEF"[c&15])
    }
  }
  return string(t)
}

func url_encode(val url.Values, is_pay bool) string {
  if val == nil {
    return ""
  }
  keys := make([]string, 0, len(val))
  for k := range val {
    keys = append(keys, k)
  }
  sort.Strings(keys)
  var buf string
  for _, k := range keys {
    vs := val[k]
    for _, v := range vs {
      if len(buf) > 0 {
        buf += "&"
      }
      if is_pay {
        buf += k + "=" + pay_rep_value(v) 
      } else {
        buf += k + "=" + v
      }
    }
  }
  return url.QueryEscape(buf)
}

type RandomString struct {
  _s []string
}

func NewRandomString(s []string) *RandomString {
  return &RandomString{s}
}

func (this *RandomString) String() string {
  _s_len := len(this._s)
  switch _s_len {
  case 0:
    return ""
  default:
    rand.Seed(time.Now().UnixNano())
    index := rand.Intn(_s_len)
    return this._s[index]
  }
}

